<!--#include file="Credenciales.asp" -->
<script language="vbscript" runat="server">

function  GetDatar(sql)
On Error Resume Next
  Dim adLockReadOnly, adUseClient, adOpenForwardOnly, sqlCommandTimeout
  adLockReadOnly = 1
  adOpenForwardOnly = 0
  adUseClient=3
  sqlCommandTimeout = 600

  dbserver = ServerName()
  dbUser = UserdBD()
  dbPassword = PasswordBD()
  database = WhichBD()
  dataString = "DRIVER={SQL Server};SERVER=" & dbserver & ";UID=" & dbUser & ";" & _
    "PWD=" & dbPassword & ";DATABASE=" & database
  'response.Write dataStringActiveConnection
  Dim Conn, oRs
  Set Conn = Server.CreateObject("ADODB.Connection")
  Conn.Open dataString
  Conn.CommandTimeout = sqlCommandTimeout
  Set oRs = Server.CreateObject("ADOR.RecordSet")
  oRs.CursorLocation = adUseClient
  oRs.Open sql,Conn,adOpenForwardOnly,adLockReadOnly
  'response.Write oRs.ActiveConnection
  Set oRs.ActiveConnection = Nothing
  Set GetDatar = oRs
  Conn.Close()
  Set Conn = Nothing
End function  

'-----------------------------------------------
'- Inserta un registro                           -
'-----------------------------------------------
function InsertarRegistro(table, keyValue)
'On Error Resume Next

  'For Each key In keyValue    
    'Response.Write (key & ": " & keyValue(key) & "<br>")
  'Next
  'Response.End

  dbserver = ServerName()
  dbUser = UserdBD()
  dbPassword = PasswordBD()
  database = WhichBD()
  Set objConnection = Server.CreateObject("ADODB.Connection") 
  dataString = "DRIVER={SQL Server};SERVER=" & dbserver & ";UID=" & dbUser & ";" & _
    "PWD=" & dbPassword & ";DATABASE=" & database
  objConnection.Open dataString
  

  counter = 0
  columns = " ("
  placeholders = " (" 
  'columns = columns & Join(keyValue.Keys, ", ") & ") "
  
  For Each key In keyValue    
    columns = columns & key 
    placeholders = placeholders & "?"
    if counter < KeyValue.Count - 1 then
      columns = columns & ", " 
      placeholders = placeholders & ", "
    end if
    counter = counter + 1
  Next
  columns = columns & ") "
  placeholders = placeholders & ");"
  
  strSql = "INSERT INTO " & table & columns & "values" & placeholders
  set objCommand = Server.CreateObject("ADODB.Command") 
  objCommand.ActiveConnection = objConnection
  objCommand.CommandText = strSql 
  counter = 0
  For Each key In keyValue    
    objCommand.Parameters(counter).value = keyValue(key) 
    counter = counter + 1
  Next
  Set objSearchResults = objCommand.Execute()
  InsertarRegistro = objSearchResults
End Function

'-----------------------------------------------
'- Edita un registro                           -
'-----------------------------------------------
function EditarRegistro(table, keyValue, keyTable, id)
  'For Each key In keyValue    
    'Response.Write (key & ": " & keyValue(key) & "<br>")
  'Next
  'Response.End
  dbserver = ServerName()
  dbUser = UserdBD()
  dbPassword = PasswordBD()
  database = WhichBD()
  Set objConnection = Server.CreateObject("ADODB.Connection") 
  dataString = "DRIVER={SQL Server};SERVER=" & dbserver & ";UID=" & dbUser & ";" & _
    "PWD=" & dbPassword & ";DATABASE=" & database
  objConnection.Open dataString
  
  counter = 0
  columns = ""
  
  For Each key In keyValue    
    columns = columns & key&"=?"
    if counter < KeyValue.Count - 1 then
      columns = columns & ", " 
    end if
    counter = counter + 1
  Next
  
  strSql = "UPDATE " & table & " SET " & columns & " WHERE " & keyTable&"=?"
  set objCommand = Server.CreateObject("ADODB.Command") 
  objCommand.ActiveConnection = objConnection
  objCommand.CommandText = strSql 
  counter = 0
  For Each key In keyValue    
    objCommand.Parameters(counter).value = keyValue(key) 
    counter = counter + 1
  Next
  objCommand.Parameters(counter).value = id 
  Set objSearchResults = objCommand.Execute()
End Function

Function RegistrarInsumosHistorial(insumoId, accion, comentarioAccion)
  
  'insertar InsumosModificacion
  'query select current insumo
  qInsumo = "SELECT * FROM Insumos WHERE ins_id=" & insumoId
  insumoRecord = GetDatar(qInsumo)

  'TODO: Validacion si hizo por lo menos un cambio

  Set keyValue = Server.CreateObject("Scripting.Dictionary")
  ' TODO: setear las variables de acuerdo a tipo de indicador NO usado a Cero 0
  keyValue("ins_id") = insumoRecord("ins_id")
  keyValue("ins_mod_codigo") = insumoRecord("ins_codigo")
  keyValue("ins_mod_fecha_requerimiento") = insumoRecord("ins_fecha_requerimiento")
  keyValue("car_id") = insumoRecord("car_id")
  keyValue("ins_mod_perfil") = insumoRecord("ins_perfil")
  keyValue("ins_mod_puesto") = insumoRecord("ins_puesto")
  keyValue("ins_mod_cant_existente") = insumoRecord("ins_cant_existente")
  keyValue("ins_mod_cant_requerida") = insumoRecord("ins_cant_requerida")
  keyValue("ins_mod_cant_certificada") = insumoRecord("ins_cant_certificada")
  keyValue("ins_mod_costo_unitario") = insumoRecord("ins_costo_unitario")
  keyValue("ins_mod_costo_total") = insumoRecord("ins_costo_total")
  keyValue("ins_mod_fecha_inicio") = insumoRecord("ins_fecha_inicio")
  keyValue("ins_mod_fecha_conclusion") = insumoRecord("ins_fecha_conclusion")
  keyValue("ins_mod_duracion") = insumoRecord("ins_duracion")
  keyValue("ins_mod_detalle") = insumoRecord("ins_detalle")
  keyValue("ins_mod_caracteristicas") = insumoRecord("ins_caracteristicas")
  keyValue("ins_mod_unidad_medida") = insumoRecord("ins_unidad_medida")
  keyValue("ins_mod_modalidad") = insumoRecord("ins_modalidad")
  keyValue("ins_mod_cargo") = insumoRecord("ins_cargo")
  keyValue("ins_mod_objetivo") = insumoRecord("ins_objetivo")
  keyValue("ins_mod_actividades") = insumoRecord("ins_actividades")
  keyValue("ins_mod_indicador") = insumoRecord("ins_indicador")
  keyValue("ins_mod_evaluador") = insumoRecord("ins_evaluador")
  keyValue("ins_mod_productos") = insumoRecord("ins_productos")
  keyValue("ins_mod_enero") = insumoRecord("ins_enero")
  keyValue("ins_mod_febrero") = insumoRecord("ins_febrero")
  keyValue("ins_mod_marzo") = insumoRecord("ins_marzo")
  keyValue("ins_mod_abril") = insumoRecord("ins_abril")
  keyValue("ins_mod_mayo") = insumoRecord("ins_mayo")
  keyValue("ins_mod_junio") = insumoRecord("ins_junio")
  keyValue("ins_mod_julio") = insumoRecord("ins_julio")
  keyValue("ins_mod_agosto") = insumoRecord("ins_agosto")
  keyValue("ins_mod_septiembre") = insumoRecord("ins_septiembre")
  keyValue("ins_mod_octubre") = insumoRecord("ins_octubre")
  keyValue("ins_mod_noviembre") = insumoRecord("ins_noviembre")
  keyValue("ins_mod_diciembre") = insumoRecord("ins_diciembre")
  keyValue("ins_mod_gestion") = insumoRecord("ins_gestion")
  keyValue("par_id") = insumoRecord("par_id")
  keyValue("auo_id") = insumoRecord("auo_id")
  keyValue("ins_mod_tipo") = insumoRecord("ins_tipo")
  keyValue("ins_mod_relacion") = insumoRecord("ins_relacion")
  keyValue("ins_mod_cert_anterior") = insumoRecord("ins_cert_anterior")
  keyValue("ins_mod_cert_actual") = insumoRecord("ins_cert_actual")
  keyValue("ins_mod_cert_saldo") = insumoRecord("ins_cert_saldo")
  keyValue("ins_mod_estado") = insumoRecord("ins_estado")
  keyValue("ins_mod_cierre") = insumoRecord("ins_cierre")
  keyValue("ins_mod_sgp") = insumoRecord("ins_sgp")
  keyValue("ins_mod_usuario_modificador") = Session("auth_granted_user_id")
  keyValue("ins_mod_accion") = accion
  keyValue("ins_mod_comentario_accion") = comentarioAccion
  InsertarRegistro "InsumosModificacion", keyValue
End Function

'Formate un numero al modo de Vscript para ser usado en JScript
Function FormatearNumero(numero)
  FormatearNumero = FormatNumber(numero,,,,0)
End Function 

'Formate un numero al modo de Vscript para ser usado en JScript
Function getActividadById(actividadId)
  qGestion = Session("auth_granted_poa_gestion")
  qActividad = "SELECT * FROM Procesos" & qGestion & " WHERE pro" & qGestion & "_id=" & actividadId & " "
  'Response.Write(qActividad)
  getActividadById = GetDatar(qActividad)  
End Function
  
Function getActividadesByApertura(aperturaId)
  qGestion = Session("auth_granted_poa_gestion")
  qActividades = "SELECT * FROM Procesos" & qGestion & " WHERE aper_id=" & aperturaId & " "
  'Response.Write(qActividades)
  getActividadesByApertura = GetDatar(qActividades)
End Function
</script>